$('#file').on('change',function(){
    var file = this.files[0];
    //创建formData对象实现二进制文件上传
    var formData = new FormData();
    //将选择到的文件添加到formData对象中
    formData.append('image',file);
    $.ajax({
        type:'post',
        url:'/upload',
        data:formData,
        processData:false,
        contentType:false,
        success:function(response){
            $('#image').val(response[0].image);
        }
    });
});

//当轮播图表单发生提交行为时
$('#slideForm').on('submit',function(){
    var formData = $(this).serialize();
    $.ajax({
        type:'POST',
        url:'/slides',
        data:formData,
        success:function(){
            location.reload();
        }
    });
    return false;
});

//获取轮播图数据 展示在页面中
$.ajax({
    type:'get',
    url:'/slides',
    success:function(response){
        var html = template('slideTpl',{data:response});
        $('#slideBox').html(html);
    }
});

//给删除按钮添加点击事件
$('#slideBox').on('click','.delete',function(){
    var id = $(this).attr('data-id');
    if(confirm('您确定要删除该数据吗？')){
        $.ajax({
            type:'delete',
            url:'/slides/'+id,
            success:function(){
                location.reload();
            }
        });
    }
    
})